package com.example.jetpackcomposebasic

import android.os.Bundle
import android.print.PrintAttributes.Margins
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.focusModifier
import androidx.compose.ui.modifier.modifierLocalProvider
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.jetpackcomposebasic.dto.Book
import com.example.jetpackcomposebasic.dto.BookFactory
import com.example.jetpackcomposebasic.ui.theme.JetpackComposeBasicTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            JetpackComposeBasicTheme {
                //BookList(books = BookFactory.list())
                //BookInfo(book = Book(title = "计算机科学", author = "大家"))
                //Body("运行成功")
                IntrinsicsConstraintHeight()
            }
        }
    }

    @Composable
    @Preview
    fun BookListPreview(){
        BookList(books = BookFactory.list())
    }

    @Composable
    fun BookList(books: List<Book>) {
        LazyColumn {
            items(books){
                BookInfo(book = it)
            }

        }
    }

    /*
    * 使用行列布局
    * */
    @Composable
    fun BookInfo(book: Book) {
        Row(
            modifier = Modifier
                .padding(all = 8.dp)
                .background(MaterialTheme.colorScheme.background)
        ) {

            Image(
                painter = painterResource(id = R.drawable.ic_launcher_background),
                contentDescription = null,
                modifier = Modifier
                    .size(40.dp)
                    .clip(CircleShape)
            )
            Spacer(modifier = Modifier.width(8.dp))

            // 作者列表是否展开
            var isExpanded by remember {
                mutableStateOf(false)
            }

            Column(
                modifier = Modifier.clickable {
                    // 点击之后 触发 展开/折叠
                    isExpanded=!isExpanded
                }
            ) {
                // 列中包含行，所以 Column 中的元素是垂直排列的
                Text(
                    text = book.title,
                    color = MaterialTheme.colorScheme.secondary
                )
                Spacer(modifier = Modifier.height(4.dp))
                Text(
                    text = book.author,
                    modifier = Modifier.padding(all=4.dp),
                    style = MaterialTheme.typography.bodySmall,
                    // 根据状态显示要展示的行数
                    maxLines = if(isExpanded) Int.MAX_VALUE else 1
                )
            }
        }
    }

    @Preview(backgroundColor = 0x00ff00)
    @Composable
    fun BookInfoPreview() = BookInfo(book = Book("java书籍名称", "老师"))

    @Composable
    fun Body(name: String) {

        Text(text = name)
    }

    @Preview
    @Composable
    fun BodyPreview() = Body(name = "你好，预览成功")
}

